package com.xizi.秋招笔试.tx;

/**
 * @author xizizzz
 * @description: TODO
 * @date 2021-8-22下午 08:12
 */
public class tx1 {

    public static class ListNode {
        int val;
        ListNode next = null;

        ListNode(int val) {
            this.val = val;
        }

        @Override
        public String toString() {
            return "ListNode{" +
                    "val=" + val +
                    ", next=" + next +
                    '}';
        }
    }

    public static ListNode[] solve(int m, ListNode a) {
        ListNode[] res = new ListNode[m];
        while (a != null) {
            int index = a.val % m;
            if (res[index] == null) {
                res[index] = new ListNode(a.val);
            } else {
                ListNode tmp = res[index];
                ListNode head = tmp;
                while (head.next != null) {
                    head = head.next;
                }
                head.next = new ListNode(a.val);
                res[index] = tmp;
            }
            a = a.next;
        }
        return res;
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(0);
        head.next = new ListNode(1);
        head.next.next = new ListNode(3);
        final ListNode[] res = solve(5, head);
        for (int i = 0; i < res.length; i++) {
            if (res[i] == null) {
                continue;
            } else {
                while (res[i] != null) {
                    System.out.println(res[i].val);
                    res[i] = res[i].next;
                }
            }
        }
    }
}
